﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TeenyGrab.Interfaces;
using TeenyGrab.Objects;
using TeenyGrab.Helpers;
using System.Runtime.InteropServices;
using System.Drawing;

namespace TeenyGrab.Inputs
{
    [Serializable]
    public class WindowInput : IInput
    {
        public bool Configurable { get { return true; } set { } }

        public bool ForegroundWindow { get; set; }
        public IntPtr Window { get; set; }

        [DllImport("user32.dll")]
        static extern IntPtr GetForegroundWindow();

        [DllImport("user32.dll")] 
        static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

        public string Name { get { return "Window"; } set { } }

        public string Description { get { return "Takes a screenshot of a specific window."; } set { } }

        public WindowInput()
        {
            ForegroundWindow = true;
        }

        public void Configure()
        {
            var config = new ConfigureWindowInputWindow(this);
            config.ShowDialog();
        }

        public ImageObject Get()
        {
            var screenCapture = new ScreenCapture();
            var image = null as Image;

            if (ForegroundWindow)
            {
                image = screenCapture.CaptureWindow(GetForegroundWindow());
            }
            else
            {
                image = screenCapture.CaptureWindow(Window);
            }

            return new ImageObject()
            {
                Image = image,
            };
        }
    }
}
